/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Listener;


import br.senai.helper.HibernateHelper;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Fernando
 */
public class SessionFactoryManager implements ServletContextListener {

    @Override
  public void contextInitialized(ServletContextEvent sce)  {
	try {

            SessionFactory sf  = new AnnotationConfiguration().configure().buildSessionFactory();
	    HibernateHelper.setSessionFactory(sf);
            
           /* Configuration cfg = new Configuration();
	    cfg.configure();
	    SessionFactory sf = cfg.buildSessionFactory();
	    HibernateHelper.setSessionFactory(sf);
	   */// log.info("SessionFactory inicializada");
	} catch (HibernateException hbe) {
            throw new HibernateException("Erro ao abrir session Factory" + hbe.getMessage());
//	    log.fatal("Impossivel abrir SessionFactory", hbe);
	}
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
	try {
	    SessionFactory sf = HibernateHelper.getSessionFactory();
	    if (!sf.isClosed())
		sf.close();
	} catch (HibernateException hbe) {
//	    log.error("Impossivel fechar SessionFactory", hbe);
	}
    }
}
